package de.tabuma.adminarea.logik;

import java.util.logging.*;
import java.lang.reflect.*;

/**
 * Generell methods for object handling.
 * @author $Author$
 * @version $Revision$
 * @since $Date$
 */
public class ObjectMethods {

    private static Logger log = Logger.getLogger(ObjectMethods.class.getName());

    /**
     * Giving a string list of an object.
     * @param object Object that will be changed to String
     * @return
     * @throws java.lang.NoSuchMethodException
     * @throws java.lang.IllegalAccessException
     * @throws java.lang.reflect.InvocationTargetException
     */
    public static String toString(Object object) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        String logText = "className:"+object.getClass().getSimpleName() +" (";
        for(int i=0;i<object.getClass().getDeclaredFields().length;i++) {
            Field field = object.getClass().getDeclaredFields()[i];
            String fieldName = field.getName();
            try {
                if(fieldName!=null) {
                    logText += field.getName()+":"+object.getClass().getMethod(getGetter(fieldName)).invoke(object)+";";
                }
            }
            catch(Exception e) {}
        }
        logText += ")";
        return logText;
    }

    /**
     * @param fieldName
     * @return
     */
    public static String getSetter(String fieldName) {
        Character a = fieldName.charAt(0);
        a = Character.toUpperCase(a);
        return "set" + a + fieldName.substring(1);
    }

    /**
     * @param fieldName
     * @return
     */
    public static String getGetter(String fieldName) {
        Character a = fieldName.charAt(0);
        a = Character.toUpperCase(a);
        return "get" + a + fieldName.substring(1);
    }

    /**
     * @param fieldName
     * @return
     */
    public static String getIs(String fieldName) {
        Character a = fieldName.charAt(0);
        a = Character.toUpperCase(a);
        return "is" + a + fieldName.substring(1);
    }
}